"use strict";
import axios ,{AJAX_START,AJAX_SUCCESS,AJAX_ERROR} from  "../../lib/networkAxios";
import api from  "../../api";
import * as type from "../mutation-types";
import qs from "qs"

//--------------------
//state
//--------------------
export const state = {
    adList:{
        page:0,
        items:[],
        count:0
    }
};
//--------------------
//getters
//--------------------
export const getters = {
    adLists:state => state.adList.items
};
//--------------------
//actions
//--------------------
const adListPost=({commit},adList)=>{
    commit(type.QUERY_ADLIST_POST,adList)
};

export const actions = {
    adListPostDispatch ({commit},page) {
        adListPost({commit},{status:AJAX_START})
        return axios.post(api.HOME_AD_LIST,qs.stringify(page)).then(res=>{
            adListPost({commit},{status:AJAX_SUCCESS,
                items:res.data.objs,
                count:res.data.count,
                msg:res.msg});
            return  Promise.resolve(res)
        }).catch(res=>{
            adListPost({commit},{status:AJAX_ERROR,msg:res.msg});
            return  Promise.reject(res)
        })
    }

};
//--------------------
//Mutations Handle
//--------------------
const adListPostMutationHandle =(state,action)=>{


    switch (action.status){
        case AJAX_START:
            console.log("AJAX_START")
           return state.adList = Object.assign({},state.adList,{
                isAxiosing: true
            });
        case AJAX_SUCCESS:
            console.log("AJAX_SUCCESS")
            console.log(state)
            console.log(action.items)

            return  state.adList = Object.assign({},state.adList, {
                isAxiosing: false,
                items: state.adList.items.concat(action.items),
                count:action.count,
                msg:action.msg
            });
        case AJAX_ERROR:
            return  state.adList = Object.assign({},state.adList, {
                isAxiosing: false,
                msg:action.msg
            })
    }
};


//--------------------
//Mutations
//--------------------
export const mutations = {
    [type.QUERY_ADLIST_POST](state,action) {

        adListPostMutationHandle(state,action)

    }

};

